{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "# 用户计算距离\n",
    "import scipy.spatial.distance as ssd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "|用户|商品1|商品2|商品3|商品4|商品5|\n",
    "|:---|:---|:---|:---|:---|:---|\n",
    "|用户1|1分|4分|2分|1分|？|\n",
    "|用户2|2分|4分|2分|1分|5分|\n",
    "|用户3|5分|1分|5分|4分|2分|\n",
    "|用户4|2分|5分|3分|4分|5分|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 用户特征向量\n",
    "r1 = np.array([1,4,2,1])\n",
    "r2 = np.array([2,4,2,1])\n",
    "r3 = np.array([5,1,5,4])\n",
    "r4 = np.array([2,5,3,4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Pearson相关系数\n",
    "def sim(v1,v2):\n",
    "    # 用户1和用户2的平均打分\n",
    "    v1_mu = sum(v1) / len(v1)\n",
    "    v2_mu = sum(v2) / len(v2)\n",
    "    # 特征向量去均值\n",
    "    v1_ = v1 - v1_mu\n",
    "    v2_ = v2 - v2_mu\n",
    "    # 返回相关系数\n",
    "    return 1 - ssd.cosine(v1_,v2_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "用户1与用户2的相似度： 0.936585811581694\n",
      "用户1与用户3的相似度： -0.8716019289105665\n",
      "用户1与用户4的相似度： 0.7302967433402214\n"
     ]
    }
   ],
   "source": [
    "# 计算用户1和2、3、4用户的相关系数\n",
    "sim1_2 = sim(r1,r2) \n",
    "sim1_3 = sim(r1,r3) \n",
    "sim1_4 = sim(r1,r4) \n",
    "print('用户1与用户2的相似度：',sim1_2)\n",
    "print('用户1与用户3的相似度：',sim1_3)\n",
    "print('用户1与用户4的相似度：',sim1_4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.0 2.25 3.75 3.5\n"
     ]
    }
   ],
   "source": [
    "# 计算用户打分均值\n",
    "r1_mu = sum(r1) / len(r1)\n",
    "r2_mu = sum(r2) / len(r2)\n",
    "r3_mu = sum(r3) / len(r3)\n",
    "r4_mu = sum(r4) / len(r4)\n",
    "print(r1_mu,r2_mu,r3_mu,r4_mu)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "计算后的预测分数是： 4.047032197970447\n"
     ]
    }
   ],
   "source": [
    "# 带入公式，预测打分\n",
    "predict_rating = r1_mu + ((5 - r2_mu) * sim1_2 + (2 - r3_mu) * sim1_3 + (5 - r4_mu) * sim1_4) / (np.abs(sim1_2) + np.abs(sim1_3) + np.abs(sim1_4))\n",
    "print('计算后的预测分数是：',predict_rating)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最终预测打分： 4.047032197970447\n"
     ]
    }
   ],
   "source": [
    "# 越界问题，评分最低0分，最高5分\n",
    "# predict_rating > 5  --> predict_rating=5\n",
    "# predict_rating < 0  --> predict_rating=0\n",
    "predict_rating = np.clip(predict_rating,0,5) # 限制输入的值\n",
    "print('最终预测打分：',predict_rating)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
